package zj.thread;

import android.content.Context;

import org.json.JSONException;
import org.json.JSONObject;

import zj.dao.DBHelper;
import zj.http.ZjHttpRequest;
import zj.pojo.ZjObject;

/**
 * Created by xq17 on 2017/5/29.
 */

public class ZjDaoThread extends Thread {
    public ZjDaoThread(Context context, ZjObject zjObject, Class<?> outClass) {
        this.context = context;
        this.zjObject = zjObject;
        this.outClass = outClass;
    }
    Context context;
    ZjObject zjObject;
    Class<?> outClass;

    @Override
    public void run() {
        super.run();
        JSONObject jsonObject = ZjHttpRequest.post(zjObject);
        try {
            if (jsonObject.getString("RESULT").equals("S")){
                ZjObject outObject = (ZjObject) outClass.newInstance();
                outObject.JsonObjectToObject(jsonObject);
                DBHelper dbHelper = new DBHelper(context);
                dbHelper.save(outObject);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }

    }
}
